package com.ddn.service.personnel.impl;

import com.ddn.mapper.personnel.DeptMapper;
import com.ddn.model.personnel.Dept;
import com.ddn.model.personnel.DeptVO;
import com.ddn.model.personnel.Station;
import com.ddn.service.personnel.DeptService;
import com.ddn.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author Bikakaso
 * @date 2023/1/1
 * ddn_hotel
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    // 部门
    // 查询全部 && 模糊查询
    @Override
    public ResultData findAll(HashMap<String, Object> map) {
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        System.out.println("map = " + map);
        String ttt = (String) map.get("name");
        map.put("ttt",ttt);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<DeptVO> list = deptMapper.findAll(map);
        PageInfo<DeptVO> info = new PageInfo<>(list);
        if (list != null) {
            return ResultData.ok(list,info.getTotal());
        }
        return ResultData.fail();
    }

    @Override
    public ResultData add(HashMap<String,Object> map) {
        String manager = (String) map.get("manager");
        System.out.println("map = " + map);
        Integer ids = deptMapper.selectId(manager);
        if (ids != null) {
            map.put("ids",ids);
            int i = deptMapper.add(map);
            if (i > 0) {
                return ResultData.ok();
            }
        }
//        System.out.println("id======= = " + id);

        return ResultData.fail();
    }

    @Override
    public ResultData deleteById(int id) {
        int i = deptMapper.deleteById(id);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData deleteBatch(int[] ids) {
        int i = deptMapper.deleteBatch(ids);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData edit(Dept dept) {
        int i = deptMapper.edit(dept);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData edit2(HashMap<String, Object> map) {
        Integer ids = (Integer) map.get("ids");
        if (ids != null) {
            int i = deptMapper.edit2(map);
            if (i > 0) {
                return ResultData.ok();
            } else {
                return ResultData.fail();
            }
        }
        return ResultData.fail();
    }


    @Override
    public Integer findId(HashMap<String, Object> map) {
        Integer id = deptMapper.findId(map);
        return id;
    }

    @Override
    public ResultData test() {
        List<Dept> list = deptMapper.test();
        if (list != null) {
            return ResultData.ok(list);
        }
        return ResultData.fail();
    }

    @Override
    public ResultData select(int id) {
        Dept dept = deptMapper.select(id);
        if (dept != null) {
            return ResultData.ok(dept);
        }
        return ResultData.fail();
    }
}
